package sysmgr.controller;

import com.alibaba.fastjson.JSON;
import sysmgr.bean.BookCommodity;
import sysmgr.bean.Page;
import sysmgr.bean.ResponseDate;
import sysmgr.service.StaffDateService;
import sysmgr.service.impl.StaffDateServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.Book;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 类的描述：
 *      后端向前端传送json数据的控制器
 *
 * @author  cwn
 * @since 2020/12/24 9:17
 */
public class StaffDateController extends HttpServlet {

    /**
     * 实例化StaffDateService对象
     */
    private StaffDateService staffDateService = new StaffDateServiceImpl();

    /**
     * 重写doGet方法
     *      不进行逻辑处理，直接调用doPost方法
     *
     * @param req 请求对象
     * @param resp 相应对象
     * @throws ServletException Servlet异常
     * @throws IOException IO流异常
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    /**
     * 重写doPost方法
     *
     * @param request 请求对象
     * @param response 相应对象
     * @throws ServletException Servlet异常
     * @throws IOException IO流异常
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端要传入的要调用哪个方法的名称
        String method = request.getParameter("method");
        //执行查询所有的方法
        if ("queryAll".equals(method)) {
            this.queryAll(request, response);
            return;
        }
        if ("deleteAll".equals(method)){
            this.deleteAll(request,response);
        }
//        if("insertAll".equals(method)){
//            this.insertAll(request,response);
//        }
        if("updateAll".equals(method)){
            this.updateAll(request,response);
        }

    }

     //查询所有的数据
    /**
     *
     * @param request 请求对象
     * @param response 响应对象
     * @throws ServletException servlet异常处理对象
     * @throws IOException IO流异常处理
     */
    private void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //创建分页对象
        Page page = new Page();
        //前端要传入current(名称必须为current)当前页
        String currentString = request.getParameter("current");
        //每页显示的大小
        String sizeString = request.getParameter("size");
        //获取传入的书本名称，用于进行条件查询
        String bookName = request.getParameter("bookName");
        //创建书本信息实体类，用来存放接收到书本名称信息
        BookCommodity bookCommodity = new BookCommodity();
        if(bookName!=null) {
            //前端获取到的书本名称封装到实体类中
            bookCommodity.setBookname(bookName);
        }
        //创建Integer类型对象，用来接收转换后的数据，默认值是1和5
        Integer current = 1;
        Integer size = 5;
        //判断传入的是否为空，如果不为空就进行类型的转换Stirng --> Integer
        if ( sizeString != null) {
            size = Integer.parseInt(sizeString);
        }
        if (currentString != null) {
            current = Integer.parseInt(currentString);
        }
        //对传入的分页数据封装
        page.setCurrent(current);
        page.setSize(size);

        //获取查询到的数据的数据
        List<BookCommodity> staffDateList = staffDateService.queryStaffDateAll(page, bookCommodity);
        //将获取到的数据封装到响应数据对象中
        ResponseDate responseDate = new ResponseDate();
        responseDate.setMsg("操作成功");
        responseDate.setData(staffDateList);
        //将响应数据对象转换为json格式
        String json = JSON.toJSONString(responseDate);
        //通过响应对象获取打印输出流

        response.setContentType("application/json;charset=UTF-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.write(json);
    }
    public void deleteAll(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        //创建分页对象
        Page page = new Page();
        //前端要传入current(名称必须为current)当前页
        String currentString = request.getParameter("current");
        //每页显示的大小
        String sizeString = request.getParameter("size");
        //获取传入的书本名称，用于进行条件删除
        String bookId = request.getParameter("id");
        //创建书本信息实体类，用来存放接收到书本名称信息
        BookCommodity bookCommodity = new BookCommodity();
        //前端获取到的书本名称封装到实体类中
        if (bookId!=null){
            bookCommodity.setId(Integer.parseInt(bookId));
        }
        //获取查询到的数据的数据
        String staffDateList = staffDateService.deleteStaffDateAll(bookCommodity);
        //将获取到的数据封装到响应数据对象中
        ResponseDate responseDate = new ResponseDate();
        responseDate.setMsg("操作成功");
        responseDate.setData(staffDateList);
        //将响应数据对象转换为json格式
        String json = JSON.toJSONString(responseDate);
        //通过响应对象获取打印输出流

        response.setContentType("application/json;charset=UTF-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.write(json);

    }
    public void updateAll(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        //创建分页对象
        Page page = new Page();
        //前端要传入current(名称必须为current)当前页
        String currentString = request.getParameter("current");
        //每页显示的大小
        String sizeString = request.getParameter("size");
        //获取传入的书本名称，用于进行条件删除
        String BookName = request.getParameter("bookname");
        String bookId = request.getParameter("id");
        //创建书本信息实体类，用来存放接收到书本名称信息
        BookCommodity bookCommodity = new BookCommodity();
        //前端获取到的书本名称封装到实体类中
        if (BookName!=null && bookId!=null){
            bookCommodity.setBookname(BookName);
            bookCommodity.setId(Integer.parseInt(bookId));
        }
        //获取查询到的数据的数据
        String staffDateList = staffDateService.updateStaffDateAll(bookCommodity);
        //将获取到的数据封装到响应数据对象中
        ResponseDate responseDate = new ResponseDate();
        responseDate.setMsg("操作成功");
        responseDate.setData(staffDateList);
        //将响应数据对象转换为json格式
        String json = JSON.toJSONString(responseDate);
        //通过响应对象获取打印输出流

        response.setContentType("application/json;charset=UTF-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.write(json);

    }


    }
